package liangtWorkSpace.cloneMysql;

import java.io.File;

public class CloneMysql {
    //mysqldump -h端口号 -u用户 -p密码 数据库 > d:/test.sql --备份D盘
    //备份
    public static void dataBaseDump(String port,String host,String username,String password,String databasename,String sqlname) throws Exception {
        File file = new File("F:\\test");
        if ( !file.exists() ){
            file.mkdir();
        }
        File datafile = new File(file+ File.separator+sqlname+".sql");
        if( datafile.exists() ){
            System.out.println(sqlname+"文件名已存在，请更换");
            return ;
        }
        //拼接cmd命令 mysqldump -h127.0.0.1 -P3306 -u root -p123456 cloud_ida > F:\\test\cloud.sql
        Process exec = Runtime.getRuntime().exec("cmd /c mysqldump -h"+host +" -P"+port+" -u "+username+" -p"+password+" "+databasename+" > "+datafile);
        if( exec.waitFor() == 0){
            System.out.println("数据库备份成功,备份路径为："+datafile);
        }
    }

    public static void main(String[] args) throws Exception {
        dataBaseDump("3306","127.0.0.1","root","123456","cloud_ida","cloud_ida");
    }
}
